<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { registerApl } from '@/api/register/register'

const ruleFormRef = ref<FormInstance>()
const registrationLoading =ref(false)
const checkAge = (rule: any, value: string, callback: any) => {
    if (!value) {
        return callback(new Error('账号未填'))
    }
    setTimeout(() => {
if (value.length < 6 || value.length >18) {
                callback(new Error('账号必须是6-18位字符'))
            } else {
                callback()
             }
    }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('密码未填'))
    }
     else {
        if (ruleForm.checkPass !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('checkPass')
        }
        callback()
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请再次输入密码'))
    } else if (value !== ruleForm.password) {
        callback(new Error("两次密码不一致"))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    username: '',
    password: '',
    checkPass: ''

})

const rules = reactive<FormRules<typeof ruleForm>>({
    username: [{ validator: checkAge, trigger: 'blur' }],
    password: [{ validator: validatePass, trigger: 'blur' }],
    checkPass: [{ validator: validatePass2, trigger: 'blur' }]

})

const submitForm = (formEl: FormInstance | undefined) => {
  registrationLoading.value=true
    if (!formEl) return

    formEl.validate(async (valid) => {

        if (valid) {
            //添加数据

            await registerApl(ruleForm).then((response) => {
                if (response.data.code === 200) {
                    ElMessage({
                        showClose: true,
                        message: '恭喜你！注册成功',
                        type: 'success',
                    })
                    resetForm(formEl)
                } else {
                    ElMessage({
                        showClose: true,
                        message: '账号已存在',
                        type: 'warning',
                    })
                }
            })

        }
        registrationLoading.value=false
    })
}
//清除表单
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<template>
  <div class="login-main">
    <div class="logincontent"></div>
    <div class="login-box">
      <div class="logo-title">
        <img src="../assets/logo.png" />
        <h1>花购平台</h1>
      </div>

      <el-card class="loginCard">
        <el-form :model="ruleForm" ref="ruleFormRef" :rules="rules">
          <el-row>
            <el-col :span="24">
              <h1 style="text-align: center; margin: 20px">注册</h1>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="username">
                <el-input
                  v-model="ruleForm.username"
                  placeholder="账号/手机号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="password">
                <el-input
                  v-model="ruleForm.password"
                  type="password"
                  autocomplete="off"
                  placeholder="请输入密码"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="checkPass">
                <el-input
                  v-model="ruleForm.checkPass"
                  type="password"
                  autocomplete="off"
                  placeholder="请再次输入密码"
                /> </el-form-item
            ></el-col>
            <el-col :span="24">
              <el-form-item>
                <el-button
                  :loading="registrationLoading"
                  style="width: 460px"
                  type="danger"
                  @click="submitForm(ruleFormRef)"
                  >{{ registrationLoading ? "注册中..." : "立即注册" }}</el-button
                >
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item>
                <el-link
                  href="/login"
                  target="primary"
                  style="margin-top: 10px; margin-left: auto"
                  >使用已有账号登录</el-link
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<style scoped lang="less">
.logincontent {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw; /* 视口宽度 */
  height: 100vh; /* 视口高度 */
  background-size: cover; /* 关键属性：覆盖整个容器 */
  background-position: center; /* 图片居中显示 */
  background-repeat: no-repeat;
  z-index: -1; /* 确保内容在上层 */
  filter: blur(8px); /* 标准语法 */
  background-image: url("../assets/花购平台登录背景图.png");
}
.login-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh; /* 视口高度 */

  min-height: 600px;
  .loginCard {
    border-radius: 20px;
    text-align: center;
    width: 500px;
    background-color: aliceblue;
  }
}
.logo-title {
  width: 500px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  img {
    display: inline-block;
    width: 80px;
  }
  h1 {
    display: inline-block;
    color: #ff0060;
    font-family: cursive;
  }
}
.login-main {
  min-width: 1000px;
}
</style>
